# -*- coding: utf-8 -*-
import traceback

from pubsub import pub

from zhi_dao.ui.zhi_dao_ui import ZhiDaoUI
from zhi_dao.zhidao_const import event_course_read_success
from zhi_dao.zhidao_const import event_login
from zhi_dao.reader.zhidao_course_reader import ZhiDaoCourseReader
from zhi_dao.reader.zhidao_section_reader import ZhiDaoSectionReader
from zhi_dao.zhidao_login import ZhiDaoLogin
from threading import Thread


def start(username, password):
    print("收到登录成功消息")
    try:
        # 异步登录并读取课程列表
        Thread(target=read_sources, args=(username, password)).start()

        # # # 测试题监控
        # topic_monitor_thread = Thread(target=topic_monitor.monitor, args=())
        # topic_monitor_thread.start()
        # # # 章节监控，视频播放完成后自动切换下一个章节
        # course_monitor_thread = Thread(target=course_monitor.monitor, args=())
        # course_monitor_thread.start()

    except Exception as e:
        print(f"An error occurred: {e}")
        traceback.TracebackException()
    finally:
        # 关闭浏览器
        print("done")


def read_sources(username, password):
    # 登录
    ZhiDaoLogin(username, password).login()
    ZhiDaoSectionReader()
    zhi_dao_course_reader = ZhiDaoCourseReader()
    zhi_dao_course_reader.read_sources_from_http()
    courses = zhi_dao_course_reader.get_courses()
    # 发布读取完成事件
    pub.sendMessage(event_course_read_success, courses=courses)


if __name__ == '__main__':
    pub.subscribe(start, event_login)
    ZhiDaoUI()






